#pragma once

using namespace System;
using namespace System::Collections;
using namespace System::ServiceProcess;
using namespace System::ComponentModel;

#include "FileThread.h"
#include "DatabaseThread.h"

namespace SampleService {

	/// <summary>
	/// Summary for SampleServiceWinService
	/// </summary>
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	public ref class SampleServiceWinService : public System::ServiceProcess::ServiceBase
	{
	public:
		SampleServiceWinService()
      : fileWriting(NULL)
      , databaseWriting(NULL)
		{
			InitializeComponent();
		}
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~SampleServiceWinService()
		{
			if (components) {
				delete components;
			}
		}

		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		virtual void OnStart(array<String^>^ args) override
		{
      if (!fileWriting) fileWriting = new FileThread;
      fileWriting->start();
      if (!databaseWriting) databaseWriting = new DatabaseThread;
      databaseWriting->start();
		}

		/// <summary>
		/// Stop this service.
		/// </summary>
		virtual void OnStop() override
		{
      fileWriting->terminate();
      databaseWriting->terminate();
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
    FileThread * fileWriting;
    DatabaseThread * databaseWriting;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = gcnew System::ComponentModel::Container();
			this->CanStop = true;
			this->CanPauseAndContinue = true;
			this->AutoLog = true;
			this->ServiceName = L"SampleServiceWinService";

      fileWriting = new FileThread();
		}
#pragma endregion
	};
}
